/*******************************************************************************
 * Copyright (c) 2007 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.extensionpoints.transformation.launch;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;

import com.cea.papyrus.extensionpoints.transformation.Activator;
import com.cea.papyrus.extensionpoints.transformation.definition.ITransformationContext;
import com.cea.papyrus.extensionpoints.transformation.definition.Transformation;
import com.cea.papyrus.extensionpoints.transformation.definition.TransformationContext;

/**
 * Configuration delegate to launch a module.
 *
 */
public class LaunchTransformationDelegate extends LaunchConfigurationDelegate  {

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
		
		// Initialize the transformation configuration for the transformation module
		ITransformationContext context = new TransformationContext(configuration);
		IProject project = context.getProject();
		Map<Object, String> properties = context.getProperties();
		
		// Initialize property path
		IPath propertyPath = null;
		
		// Start the transformation
		try {
			//create properties if necessary
			if (properties != null) {
				propertyPath = new Path(project.getLocation()+"/.uml.properties"); 
				createPropertyFile(propertyPath,properties);
			}
			
			Transformation transformation = context.getTransformation();
			transformation.getModule().createTarget(context, monitor, properties, mode);


		} catch (Exception e) {
			Activator.log(e);
		} 	

		//clean files
		if (properties != null) {
			File file = propertyPath.toFile();
			file.delete();
		}

		//refresh project
		project.refreshLocal(IResource.DEPTH_INFINITE, null);
	}

	/**
	 * Creates the temporary properties file.
	 * @param path
	 * @param propertyMap
	 */
	private void createPropertyFile(IPath path, Map<Object, String> propertyMap) {
		try {
			File file = path.toFile();
			FileWriter out = new FileWriter(file.getAbsolutePath());
			for (Iterator<Map.Entry<Object, String>> iter = propertyMap.entrySet().iterator(); iter.hasNext();) {
				Map.Entry<Object, String> property = iter.next();
				out.write(property.getKey()+"="+property.getValue()+"\n");
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
